package com.zsyh.jiujiu.home;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.androidkun.xtablayout.XTabLayout;
import com.example.zyh.toolslib.base.BaseActivity;
import com.example.zyh.toolslib.base.BaseFragment;
import com.example.zyh.toolslib.utils.SPUtils;
import com.example.zyh.toolslib.widget.TitleBar;
import com.google.common.util.concurrent.ListenableFutureTask;
import com.google.gson.Gson;
import com.lwb.retrofit.RetrofitTask;
import com.zsyh.jiujiu.Api;
import com.zsyh.jiujiu.R;
import com.zsyh.jiujiu.bean.MessageEvn;
import com.zsyh.jiujiu.bean.MyGoodBean;
import com.zsyh.jiujiu.fragment.magager.NoPassFragment;
import com.zsyh.jiujiu.fragment.magager.shangjiaFragment;
import com.zsyh.jiujiu.fragment.magager.shengheFragment;
import com.zsyh.jiujiu.fragment.magager.xiajiaFragment;
import com.zsyh.jiujiu.fragment.orderFragment.AllOrderFragment;
import com.zsyh.jiujiu.fragment.orderFragment.DaifahuiFragment;
import com.zsyh.jiujiu.fragment.orderFragment.DaihuifuFragment;
import com.zsyh.jiujiu.fragment.orderFragment.DaishouhuoFragment;
import com.zsyh.jiujiu.fragment.orderFragment.DdaiquerenFragment;
import com.zsyh.jiujiu.net.MyFragementObserver;
import com.zsyh.jiujiu.net.MyObserver;
import com.zsyh.jiujiu.weight.NoScrollViewPager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @ProjectName: livvehelp
 * @Package: com.zsyh.lsb.ui
 * @ClassName:
 * @Description: java类作用描述
 * @Author: zsyh
 * @CreateDate: 2020/9/7 11:59
 * @UpdateUser: liangwanbo
 * @UpdateDate: 2020/9/7 11:59
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class GoodManagerActivity extends BaseActivity {

    @BindView(R.id.title_bar)
    TitleBar titleBar;
    @BindView(R.id.xTablayout)
    XTabLayout xTablayout;
    @BindView(R.id.viewpager)
    NoScrollViewPager viewpager;

    private List<BaseFragment> fragmentList;
    private int selectPos = 0;

    List<String> listtitle=new ArrayList<>();
    private String shangnum,xianum,shenghemun,quxiaonum;
    ViewPagerAdapter viewPagerAdapter;


    @Override
    protected int initLayout() {
        return R.layout.activity_guanli;
    }

    @Override
    protected void initView() {
        EventBus.getDefault().register(this);
    }

    @Override
    protected void initData() {
        shangnum=getIntent().getStringExtra("shangnum");
        xianum=getIntent().getStringExtra("xianum");
        shenghemun=getIntent().getStringExtra("shenghemun");
        quxiaonum=getIntent().getStringExtra("quxiaonum");
        listtitle.add("上架中("+shangnum+")");
        listtitle.add("已下架("+xianum+")");
        listtitle.add("审核中("+shenghemun+")");
        listtitle.add("未通过("+quxiaonum+")");
        fragmentList=new ArrayList<>();
        fragmentList.add(shangjiaFragment.newInstance(this,0));
        fragmentList.add(xiajiaFragment.newInstance(this,1));
        fragmentList.add(shengheFragment.newInstance(this,2));
        fragmentList.add(NoPassFragment.newInstance(this,3));
         viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), fragmentList);
        viewpager.setOffscreenPageLimit(4);
        viewpager.setAdapter(viewPagerAdapter);
        xTablayout.setupWithViewPager(viewpager);
        xTablayout.getTabAt(selectPos).select();
        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                EventBus.getDefault().post(new MessageEvn("", "dong"));
            }

            @Override
            public void onPageSelected(int position) {
                EventBus.getDefault().post(new MessageEvn("", "dong"));
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                EventBus.getDefault().post(new MessageEvn("", "dong"));
            }
        });
    }

    @Override
    protected void initEvent() {
        titleBar.setLeftLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private class ViewPagerAdapter extends FragmentPagerAdapter {

        private List<BaseFragment> fragmentList;

        public ViewPagerAdapter(FragmentManager fm, List<BaseFragment> fragmentList) {
            super(fm);
            this.fragmentList = fragmentList;
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return listtitle.get(position);
        }
    }

    /**
     * 我的商品
     */
    public void MyGood(String userid,String stattus){
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .getMyGoods(userid,stattus)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyObserver(this,false) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Log.e("wode",JsonStr);
                        Gson gson=new Gson();
                        MyGoodBean goodBean=gson.fromJson(JsonStr,MyGoodBean.class);
                        List<MyGoodBean.DataBean> list=new ArrayList<>();
                        for(int i=0;i<goodBean.getData().size();i++){
                            list.add(goodBean.getData().get(i));
                        }
                        listtitle.set(0,"上架中("+list.size()+")");
                        viewPagerAdapter.notifyDataSetChanged();
                    }
                });

    }


    /**
     * 我的商品
     */
    public void MyGoodxia(String userid,String stattus){
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .getMyGoods(userid,stattus)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyObserver(this,false) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Log.e("wode",JsonStr);
                        Gson gson=new Gson();
                        MyGoodBean goodBean=gson.fromJson(JsonStr,MyGoodBean.class);
                        List<MyGoodBean.DataBean> list=new ArrayList<>();
                        for(int i=0;i<goodBean.getData().size();i++){
                            list.add(goodBean.getData().get(i));
                        }
                        listtitle.set(1,"已下架("+list.size()+")");
                        viewPagerAdapter.notifyDataSetChanged();
                    }
                });

    }

    public void shenghe(String userid,String stattus){
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .getMyGoods(userid,stattus)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyObserver(this,false) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Log.e("wode",JsonStr);
                        Gson gson=new Gson();
                        MyGoodBean goodBean=gson.fromJson(JsonStr,MyGoodBean.class);
                        List<MyGoodBean.DataBean> list=new ArrayList<>();
                        for(int i=0;i<goodBean.getData().size();i++){
                            list.add(goodBean.getData().get(i));
                        }
                        listtitle.set(2,"审核中("+list.size()+")");
                        viewPagerAdapter.notifyDataSetChanged();
                    }
                });

    }

    public void quxiaole(String userid,String stattus){
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .getMyGoods(userid,stattus)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyObserver(this,false) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Log.e("wode",JsonStr);
                        Gson gson=new Gson();
                        MyGoodBean goodBean=gson.fromJson(JsonStr,MyGoodBean.class);
                        List<MyGoodBean.DataBean> list=new ArrayList<>();
                        for(int i=0;i<goodBean.getData().size();i++){
                            list.add(goodBean.getData().get(i));
                        }
                        listtitle.set(3,"未通过("+list.size()+")");
                        viewPagerAdapter.notifyDataSetChanged();
                    }
                });

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void MessageEventBus(MessageEvn messageInfo) {
        if (messageInfo.getMessage().equals("gaibian")) {
            MyGood((String) SPUtils.get(thisActivity, "userid", ""),"1");
            MyGoodxia((String) SPUtils.get(thisActivity, "userid", ""),"2");
            shenghe((String) SPUtils.get(thisActivity, "userid", ""),"3");
            quxiaole((String) SPUtils.get(thisActivity, "userid", ""),"4");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this))
            EventBus.getDefault().unregister(this);
    }

}
